package com.hz.ftl.controller;

import com.hz.ftl.bean.UserInfo;
import com.hz.ftl.util.WordUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by kevin on 2022/5/18 21:28
 */
@RestController
public class DownloadController {

    @GetMapping(value = "/browser")
    public void download(HttpServletResponse response) {
        WordUtil.getInstance().exportWordByBrowser(response, fakeDataMap(), "test.ftl", "demo.doc");
    }

    @PostMapping(value = "/save/path")
    public void downloadByFile() {
        WordUtil.getInstance().exportWordByFile(fakeDataMap(), "/Users/kevin/Desktop/freemarker/", "test.ftl", "demo.doc");
    }


    private Map<String, Object> fakeDataMap() {
        Map<String, Object> map = new HashMap<>();
        // 1.文本
        map.put("title", "演示");
        map.put("author", "张三");
        // 2.日期
        map.put("date", LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        // 3.布尔
        map.put("isBool", false);
        // 4.序列
        List<String> fruits = new ArrayList<>();
        fruits.add("橙子");
        fruits.add("苹果");
        fruits.add("香蕉");
        fruits.add("橘子");
        fruits.add("桃子");
        map.put("fruits", fruits);
        // 4.集合
        List<UserInfo> lists = new ArrayList<>();
        lists.add(new UserInfo(11, "whz1", false));
        lists.add(new UserInfo(12, "whz2", true));
        lists.add(new UserInfo(13, "whz3", false));
        lists.add(new UserInfo(14, "whz4", true));
        lists.add(new UserInfo(15, "whz5", true));
        map.put("users", lists);
        // 6.图片
        map.put("images", WordUtil.getInstance().getFakeImages());
        return map;
    }


}
